import dataclasses
import os

import weaviate
from injector import inject
from langchain_core.documents import Document
from langchain_core.vectorstores import VectorStoreRetriever
from langchain_weaviate import WeaviateVectorStore
from weaviate import WeaviateClient
from langchain_openai import OpenAIEmbeddings


@inject
class WeaviateService:
    client: WeaviateClient
    vector_store: WeaviateVectorStore

    def __init__(self):
        print(os.getenv('WEAVIATE_URL'), int(os.getenv('WEAVIATE_PORT')))
        self.client = weaviate.connect_to_local(os.getenv('WEAVIATE_URL'), int(os.getenv('WEAVIATE_PORT')))

        self.vector_store = WeaviateVectorStore(
            client=self.client,
            index_name="DatasetTest",
            text_key="text",
            embedding=OpenAIEmbeddings(model="text-embedding-3-small")
        )

    def get_retriever(self) -> VectorStoreRetriever:
        print('get_retriever')

        return self.vector_store.as_retriever()

    @classmethod
    def combine_documents(cls, documents: list[Document]) -> str:
        print('combine_documents')

        return "\n\n".join([document.page_content for document in documents])
